package com.sgcc.operate.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.common.core.domain.R;
import com.sgcc.common.security.service.TokenService;
import com.sgcc.system.api.RemoteDictDataService;
import com.sgcc.system.api.RemoteUserService;
import com.sgcc.system.api.domain.SysUser;
import com.sgcc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.operate.domain.OMonsysStaff;
import com.sgcc.operate.service.IOMonsysStaffService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 人员系统权限Controller
 *
 * @author sgcc
 * @date 2021-03-29
 */
@RestController
@RequestMapping("/monsysstaff")
public class OMonsysStaffController extends BaseController {
    @Autowired
    private IOMonsysStaffService oMonsysStaffService;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private RemoteDictDataService remoteDictDataService;

    /**
     * 查询人员系统权限列表
     */
    //@PreAuthorize(hasPermi = "operate:monsysstaff:list")
    @GetMapping("/list")
    public TableDataInfo list(OMonsysStaff oMonsysStaff) {
        startPage();
        List<OMonsysStaff> list = oMonsysStaffService.selectOMonsysStaffList(oMonsysStaff);
        return getDataTable(list);
    }

    /**
     * 导出人员系统权限列表
     */
    //@PreAuthorize(hasPermi = "operate:monsysstaff:export")
    @Log(title = "人员系统权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OMonsysStaff oMonsysStaff) throws IOException {
        List<OMonsysStaff> list = oMonsysStaffService.selectOMonsysStaffList(oMonsysStaff);
        ExcelUtil<OMonsysStaff> util = new ExcelUtil<OMonsysStaff>(OMonsysStaff.class);
        util.exportExcel(response, list, "monsysstaff");
    }

    /**
     * 获取人员系统权限详细信息
     */
    //@PreAuthorize(hasPermi = "operate:monsysstaff:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(oMonsysStaffService.selectOMonsysStaffById(id));
    }

    /**
     * 新增人员系统权限
     */
    //@PreAuthorize(hasPermi = "operate:monsysstaff:add")
    @Log(title = "人员系统权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OMonsysStaff oMonsysStaff) {
        return toAjax(oMonsysStaffService.insertOMonsysStaff(oMonsysStaff));
    }

    /**
     * 修改人员系统权限
     */
    //@PreAuthorize(hasPermi = "operate:monsysstaff:edit")
    @Log(title = "人员系统权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OMonsysStaff oMonsysStaff) {
        return toAjax(oMonsysStaffService.updateOMonsysStaff(oMonsysStaff));
    }

    /**
     * 删除人员系统权限
     */
    //@PreAuthorize(hasPermi = "operate:monsysstaff:remove")
    @Log(title = "人员系统权限", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(oMonsysStaffService.deleteOMonsysStaffByIds(ids));
    }

    @PostMapping(value = "/saveByUserIds")
    public AjaxResult saveByUserIds(@RequestBody OMonsysStaff oMonsysStaff) throws Exception {
        try {
            String[] codes = oMonsysStaff.getSysCode();
            String[] idArray = oMonsysStaff.getIds();
            for (String id : idArray) {
                SysUser sysUser = remoteUserService.selectUserByUserId(id);
                if (null != sysUser && null != sysUser.getUserId()) {
                    for (String code : codes) {
                        if (oMonsysStaffService.checkMonsysStaff(id, code)) {
                            String r = remoteDictDataService.getLabel("sys_type", code);
                            if (r == null) {
                                return AjaxResult.error("服务器异常");
                            }
                            String SysCodeName = r;
                            if (SysCodeName == null) {
                                return AjaxResult.error("未知系统");
                            }
                            return AjaxResult.error(sysUser.getUserName() + "已有" + SysCodeName + "权限，请不要重复添加！");
                        }
                    }
                } else {
                    return AjaxResult.error("未找到用户");
                }
            }
            for (String code : codes) {
                oMonsysStaffService.saveByUserIds(idArray, code);//保存
            }
            return AjaxResult.success("保存人员系统权限成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存人员系统权限失败!");
        }
    }
}
